//
//  DetailSpecialViewController.m
//  小胖兔旅行指南
//
//  Created by dlios on 15/4/15.
//  Copyright (c) 2015年 小胖兔. All rights reserved.
//

#import "DetailSpecialViewController.h"
#import "MyConnection.h"
#import "MyTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "DetailViewController.h"

@interface DetailSpecialViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *table;
@property (nonatomic, retain) NSMutableArray *tableArray;

@end

@implementation DetailSpecialViewController

-(void)dealloc
{
    [_tableArray release];
    [_table release];
    
    [super dealloc];
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];
    }
    return self;
}

- (void)createTableView
{
    self.table = [[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64 - 49) style:UITableViewStylePlain] autorelease];
    [self.view addSubview:_table];
    
    _table.dataSource = self;
    _table.delegate = self;
    
    [_table registerClass:[MyTableViewCell class] forCellReuseIdentifier:@"detail"];
    [_table setSeparatorStyle:UITableViewCellSeparatorStyleNone];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self createTableView];
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:@"1" forKey:@"page"];
    
    NSString *urlString = [NSString stringWithFormat:@"%@/%ld", collection, self.tag];
    
    __block DetailSpecialViewController *my = self;
    [MyConnection connectWithUrl:urlString parmaters:dic finishBlock:^(NSData *data) {
        my.tableArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        [my.table reloadData];
    }];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200.0f;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.tableArray count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"detail"];
    NSMutableDictionary *dic = [_tableArray objectAtIndex:indexPath.row];
    NSMutableArray *array = [dic objectForKey:@"bg_pic"];
    NSString *urlString = [array objectAtIndex:0];
    NSURL *url = [NSURL URLWithString:urlString];
    [cell.image sd_setImageWithURL:url];
    
    NSString *title = [dic objectForKey:@"title"];
    [cell.title setText:title];
    
    NSString *address = [NSString stringWithFormat:@"🏁%@", [dic objectForKey:@"destination"]];
    [cell.address setText:address];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *det = [[[DetailViewController alloc] init] autorelease];
    NSMutableDictionary *dic = [_tableArray objectAtIndex:indexPath.row];
    NSNumber *num = [dic objectForKey:@"id"];
    NSInteger tag = [num integerValue];
    det.tag = tag;
    
    [self.navigationController pushViewController:det animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
